def power(x):
    return x * x


print(power(10))


def power(x, n=2):  # n=2默认参数
    s = 1
    while n > 0:
        n = n - 1
        s = s * x
    return s


print(power(5))


def enroll(name, gender='一年级'):
    print('name', name)
    print('gender', gender)


enroll('zhangsan')


def add_end(L=None):
    if L is None:
        L = []
    L.append('end')
    return L


print(add_end())


# 可变参数

def calc(numbers):
    sum = 0
    for n in numbers:
        sum = sum + n * n
    return sum


print(calc((2, 3)))


# 正常调用需要list，或者元组（可变参数在出入量前加*，允许传入0个或任意个参数，在调用函数时自动组装为一个人tuple）

def calc(*numbers):
    sum = 0
    for n in numbers:
        sum = sum + n * n
    return sum


print(calc(2, 3))
print(calc(*[2, 3]))  # 可便参数也可以传入list与元组，需要在前加上*（即将list与元组所有元素当作可变参数传入）


# 关键字参数
def person(name, age, **kw):
    print('name', name, 'age', age, 'orther', kw)


person('lisi', 18)
person('zhaowu', 33, city='beijing', num=110)  # 关键字参数自动组装为dict


# 命名关键字参数（限制关键字参数的名字）
def person(name, age, *, city, job):  # 需要特殊分隔符*，在*后面的参数被视为命名关键字参数
    print(name, age, city, job)


person('jack', 20, city='shanghai', job='teacher')


def person(name, age, *args, city, job):
    print(name, age, args, city, job)


person('张三', 22, '繁多花园', city='beijing', job='牛马')


def person(name, age, *args, **kw):
    print(name, age, args, kw)


person('小明', 90, '饭都大姐', city='hefei', home='hunan')


# 参数组合


def f1(a, b, c=0, *args, **kw):
    print('a =', a, 'b =', b, 'c =', c, 'args =', args, 'kw =', kw)


def f2(a, b, c=0, *, d, **kw):
    print('a =', a, 'b =', b, 'c =', c, 'd =', d, 'kw =', kw)


f1(1, 2)
f1(1, 23, 5)
f1(1, 2, 3, 'a', 'b')
f1(1, 2, 3, 'a', 'b', x=99)
f2(1, 2, 3, d=99, ext='none')
args = (1, 2, 3, 4)
kw = {'d': 99, 'x': '#'}
f1(*args, **kw)
args = (1, 2, 3)
kw = {'d': 88, 'x': '#'}
f2(*args, **kw)


# 小练习

def mul(*number):
    sum = 1
    for n in number:
        sum = sum * n
    return sum


print(mul(2, 3, 4))
